<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>已投项目</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script>
    <script type="text/javascript" src="./script/aui-tab.js" ></script>
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript">
        function closeWin(){

            // var goToUser = 'goToUser();';
            // api.execScript({
            //     name: 'home',
            //     script: goToUser,
            // });

            // var setSele = 'changeSelect();';
            // api.execScript({
            //     name: 'home',
            //     script: setSele,
            // });

            api.closeToWin({
                name: 'home'
            });
        };
    </script>
</head>
<body>
    <!-- 头部 -->
    <header class="aui-bar aui-bar-nav pos" id="topbar">
        <a class="aui-pull-left aui-btn" onclick="closeWin()">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title">已投项目</div>
    </header>
    <div class="header-d"></div>
    <div class="loan-list-t">
        <ul>
            <li id="back" onclick="updateBack();">回款中</li>
            <li id="ing" class="active" onclick="updateIn();">投标中</li>
            <li id="already" onclick="updateAlready();">已完成</li>
        </ul>
    </div>
    <div id="list">
        <div class="loan-list-d">
            <ul>
                <li>
                    <div class="loan-list-d-t">
                        <p>企业经营贷-0000000-000</p>
                        <i class="fa fa-angle-right"></i>
                        <span>回款中</span>
                    </div>
                    <div class="loan-list-d-l">
                        预期年化利率
                        <p>00.0<span>%</span></p>
                    </div>
                    <div class="loan-list-d-r">
                        <p>金额:0,000.00元</p>
                        <p>预期收益:00.00元</p>
                        <p>期限:0个月</p>
                        <p>时间:0000-00-00</p>
                    </div>
                    <div class="loan-list-d-h"></div>
                    <div class="loan-list-d-d">
                        剩余 0期/0期
                        <span>查看合同(0)</span>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</body>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="loan-list-d">
        <ul>
            <li>
                <div class="loan-list-d-t">
                    <p>{{=value.name}}-{{=value.deal_sn}}</p>
                    <i class="fa fa-angle-right"></i>
                    <span>{{=value.state}}</span>
                </div>
                <div class="loan-list-d-l">
                    预期年化利率
                    <p>{{=value.rate}}<span>%</span></p>
                </div>
                <div class="loan-list-d-r">
                    <p>金额:{{=value.money}}元</p>
                    <p>预期收益:{{=value.repay_money}}元</p>
                    <p>期限:{{=value.repay_time}}个月</p>
                    <p>时间:{{=value.create_date}}</p>
                </div>
                <div class="loan-list-d-h"></div>
                <div class="loan-list-d-d">
                    剩余 {{=value.wait_num}}期/{{=value.total_num}}期
                    <span>查看合同(4)</span>
                </div>
            </li>
        </ul>
    </div>
    {{~}}
</script>
<script type="text/javascript">

    apiready = function(){
        // pullRefresh();
        api.addEventListener({
            name: 'keyback'
        }, function(ret, err) {
            closeWin();
        });
        updateData(1);
    };

    // 更新数据,模板函数
    function updateData(state){
        status = state;
        // 进度提示框
        api.showProgress({
            title: '努力加载中...',
            text: '请稍等...',
            modal: false
        });

        var userid = $api.getStorage('userid');
        api.ajax({
            url: webAddress + '/member.php?ctl=app_user_center&act=user_deallist',
            method: 'post',
            data: {
                values: {
                    user_id: userid,
                    deal_status: state,
                },
            }
        },function(ret, err){
            if (ret) {
                // var ret = $api.jsonToStr(ret);
                // alert(ret);
                // 关闭下拉刷新控件
                api.refreshHeaderLoadDone();
                api.hideProgress();
                if (ret.status == 0) {
                    alert(ret.info);
                    return false;
                }else{
                    var data = ret.deals;
                    if (data == '') {
                        if (state == 4) {
                            var list = $api.byId('list');
                            list.innerHTML = '<div class="login-list-no">没有回款中的项目</div>';
                        }else if(state == 1){
                            var list = $api.byId('list');
                            list.innerHTML = '<div class="login-list-no">没有投标中的项目</div>';
                        }else{
                            var list = $api.byId('list');
                            list.innerHTML = '<div class="login-list-no">没有已完成的项目</div>';
                        }
                    }else{

                    // 修改金钱格式 已经列表右上角的状态
                    for (var i = 0; i < data.length; i++) {
                        if (state == 4) {
                            data[i].state = '回款中';
                        }else if (state == 1) {
                            data[i].state = '投标中';
                        }else{
                          data[i].state = '已完成';
                      }
                      data[i].money = outputmoney(data[i].money);
                  }
                  templateFun(data);
              }
          }
      } else {
        alert(err.msg);
        api.refreshHeaderLoadDone();
        api.hideProgress();
    }
});
    };

    // 模板赋值函数
    function templateFun(data){
        var template = $api.byId('template');
        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var user = $api.byId('list');
        $api.html(user, html);
    }

    // 下拉刷新
    function pullRefresh(){
        api.setRefreshHeaderInfo({
            // loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...'
        }, function(ret, err) {
            //在这里从服务器加载数据，加载完成后调用api.refreshHeaderLoadDone()方法恢复组件到默认状态
            updateData(status);
        });
    }

    // 回款中
    function updateBack(){
        updateData(4);
        var back = $api.byId('back');
        var ing = $api.byId('ing');
        var already = $api.byId('already');
        $api.removeCls(ing, 'active');
        $api.removeCls(already, 'active');
        $api.addCls(back, 'active');
    }

    // 投标中
    function updateIn(){
        updateData(1);
        var back = $api.byId('back');
        var ing = $api.byId('ing');
        var already = $api.byId('already');
        $api.removeCls(back, 'active');
        $api.removeCls(already, 'active');
        $api.addCls(ing, 'active');
    }

    // 已完成
    function updateAlready(){
        updateData(5);
        var back = $api.byId('back');
        var ing = $api.byId('ing');
        var already = $api.byId('already');
        $api.removeCls(back, 'active');
        $api.removeCls(ing, 'active');
        $api.addCls(already, 'active');
    }
</script>
</html>
